<?php
declare(strict_types=1);

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class Menu extends FormRequest
{
    /**
     * 授权表单请求
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * 定义验证规则
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules(): array
    {
        return [
            'pid' => 'required|integer',
            'menu_type' => 'required|in:1,2',
            'menu_name' => 'required|regex:/^[A-Za-z0-9\x{4e00}-\x{9fa5}]+$/u|between:2,20',
            'menu_url' => 'required|between:1,20',
            'menu_icon' => 'max:20',
            'menu_sort' => 'integer',
            'menu_status' => 'required|in:1,2',
        ];
    }

    /**
     * 自定义验证错误
     * @return array
     */
    public function messages(): array
    {
        return [
            'pid.required' => '父ID必须输入',
            'pid.integer' => '父ID只能是整数',

            'menu_type.required'  => '菜单类别必须输入',
            'menu_type.in'  => '菜单类别值只能是1或2',

            'menu_name.required' => '菜单名称必须输入',
            'menu_name.regex' => '菜单名称不能输入非法字符',
            'menu_name.between' => '菜单名称长度只能是2至20个字符',

            'menu_url.required' => '路由地址必须输入',
            'menu_url.between' => '路由地址长度只能是1至20个字符',

            'menu_icon.max' => '显示图标最长只能是20个字符',

            'menu_sort.integer' => '排序值只能是整数',

            'menu_status.required' => '菜单状态必须输入',
            'menu_status.between' => '菜单状态值只能是1或2',
        ];
    }
}
